using System.Collections.Generic;
using System.Linq;
using GameFrameworkLibraly.BaseObjects;
using GameFrameworkLibraly.Helpers.GeometryHelpers;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input.Touch;

namespace GameFrameworkLibraly.Controls
{
    public class ListBoxControl:Control
    {
        private int _distanceBetweenObjects;
        public int DistanceBetweenObjects
        {
            get { return _distanceBetweenObjects; }
            set
            {
                _distanceBetweenObjects = value; 
                RecalculateControlsPosition();
            }
        }
        public ListBoxControl(MyGame game, List<Control> controls, Rectangle destinationRectangle) : base(game)
        {
            Controls = controls;
            DestinationRectangle = destinationRectangle;
            RecalculateControlsPosition();
        }

        public List<Control> Controls { get; protected set; }

        private float _verticalOffset;

        public float VerticalOffset
        {
            get { return _verticalOffset; }
            protected set
            {
                _verticalOffset = value;
                RecalculateControlsPosition();
            }
        }

        private void RecalculateControlsPosition()
        {
            int offset = (int) _verticalOffset;
            foreach (var control in Controls)
            {
                control.DestinationRectangle =
                    new Rectangle((DestinationRectangle.Width - control.DestinationRectangle.Width)/2, offset,
                                  control.DestinationRectangle.Width, control.DestinationRectangle.Height);
                offset += control.DestinationRectangle.Height + DistanceBetweenObjects;
            }
            
        }

        #region Control

        /// <summary>
        /// Draws the control
        /// </summary>
        /// <param name="spriteBatch"></param>
        public override void Draw(SpriteBatch spriteBatch)
        {
            if(!Visible)
                return;
            if(DestinationRectangle.IsEmpty)
                return;
            if(BackgroundTexture!=null)
                spriteBatch.Draw(BackgroundTexture,DestinationRectangle,BackColorFilter);
            foreach (var control in Controls)
            {
                control.Draw(spriteBatch);
            }
        }

        /// <summary>
        /// Updates state of the control
        /// </summary>
        /// <param name="gameTime"></param>
        public override void Update(GameTime gameTime)
        {
            if (!Enabled)
                return;
            foreach (var control in Controls)
            {
                control.Update(gameTime);
            }
        }

        public override bool ProcessTouch(TouchLocation touchLocation)
        {
            if (!Enabled)
                return false;
            return Controls.Any(control => control.ProcessTouch(touchLocation));
        }

        public override bool ProcessGesture(GestureSample gestureSample)
        {
            if (!Enabled)
                return false;
            if (Controls.Any(control => control.ProcessGesture(gestureSample)))
                return true;
            if(gestureSample.GestureType==GestureType.VerticalDrag)
            {
                if (DestinationRectangle.Contains(gestureSample.Position.ToPoint()))
                {
                    if (!(VerticalOffset + gestureSample.Delta.Y > 0 ) &&
                         ! (VerticalOffset + gestureSample.Delta.Y <-(Controls.Sum(i=>i.DestinationRectangle.Height+DistanceBetweenObjects)-Game.Window.ClientBounds.Height )))
                        VerticalOffset += gestureSample.Delta.Y;
                    return true;
                }
            }
            return false;
        }

        #endregion
    }
}
